using System;

namespace Runtime.DesignPattern
{
    public abstract class BaseMediatorComponent : IMediatorComponent
    {
        private IMediator _mediator;
        private readonly string _componentId;

        protected BaseMediatorComponent(string componentId)
        {
            if (string.IsNullOrEmpty(componentId))
                throw new ArgumentException("Component ID cannot be null or empty", nameof(componentId));

            _componentId = componentId;
        }

        public IMediator Mediator
        {
            get => _mediator;
            set => _mediator = value;
        }

        public string ComponentId => _componentId;

        protected void SendMessage(IMediatorMessage message)
        {
            _mediator?.Send(message, this);
        }

        protected void SendMessage(string messageType, object data, string targetComponentId = null)
        {
            var message = new MediatorMessage(messageType, data, targetComponentId);
            SendMessage(message);
        }

        public abstract void HandleMessage(IMediatorMessage message);
    }
}